package de.velvettear.squirrel.adapter;

import java.util.Vector;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import de.velvettear.squirrel.R;
import de.velvettear.squirrel.adapter.PortAdapter.PortViewHolder;
import de.velvettear.squirrel.kernel.Password;

public class PasswordStoreAdapter extends BaseExpandableListAdapter {

	private Context context;
	
	private Vector<Password> groups;
		
	public PasswordStoreAdapter(Context context, Vector<Password> groups) {
		this.context = context;
		this.groups = groups;
	}
	
	public void addItem(Password dv) {
//		if (!groups.contains(dv)) {
//			groups.add(dv);
//		}
//		int index = groups.indexOf(dv);
//		if (children.size() < index + 1) {
//			children.add(new Vector<Password>());
//		}
//		children.get(index).add(dv);
	}
//	
	public Object getChild(int groupPosition, int childPosition) {
		return groups.get(groupPosition);
//		return children.get(groupPosition).get(childPosition);
	}

	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		ChildrenViewHolder childrenViewHolder;
		Password pass = groups.elementAt(groupPosition);
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.expandable_child_passwordstore, null);
			
			childrenViewHolder = new ChildrenViewHolder();
			childrenViewHolder.tv_value = (TextView) convertView.findViewById(R.id.expandable_child_passwordstore_value);
			
			convertView.setTag(childrenViewHolder);
		} else {
			childrenViewHolder = (ChildrenViewHolder) convertView.getTag();
		}
		
		childrenViewHolder.tv_value.setText(pass.m_sValue);
				
		return convertView;
	}

	public int getChildrenCount(int groupPosition) {
		return groups.size();
	}

	public Object getGroup(int groupPosition) {
		return groups.get(groupPosition);
	}

	public int getGroupCount() {
		return groups.size();
	}

	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		GroupViewHolder groupViewHolder;
		Password pass = groups.elementAt(groupPosition);
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.expandable_group_passwordstore, null);
			
			groupViewHolder = new GroupViewHolder();
			groupViewHolder.tv_name = (TextView) convertView.findViewById(R.id.expandable_group_passwordstore_name);
			
			convertView.setTag(groupViewHolder);
		} else {
			groupViewHolder = (GroupViewHolder) convertView.getTag();
		}
		
		groupViewHolder.tv_name.setText(pass.m_sName);
					
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}
	
	static class GroupViewHolder {
		TextView tv_name;
	}
	
	static class ChildrenViewHolder {
		TextView tv_value;
	}
	
}
